package thread;

import java.util.concurrent.atomic.AtomicInteger;

public class LambdaThreadDemo {
    public static void main(String[] args) {
        System.out.println("=== Thread类Lambda表达式演示 ===");
        
        // 1. 基本Lambda表达式
        Thread t1 = new Thread(() -> {
            System.out.println("线程1执行: " + Thread.currentThread().getName());
        });
        
        // 2. 带参数的Lambda（使用外部变量）
        String message = "Hello Lambda";
        Thread t2 = new Thread(() -> {
            System.out.println("线程2执行: " + message);
        });
        
        // 3. 复杂逻辑的Lambda
        AtomicInteger counter = new AtomicInteger(0);
        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                int count = counter.incrementAndGet();
                System.out.println("线程3计数: " + count);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        
        // 启动所有线程
        t1.start();
        t2.start();
        t3.start();
        
        // 主线程等待
        try {
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        System.out.println("所有线程执行完成");
    }
}